﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CS2102_Music.Logic.LogicModels;

namespace CS2102_Music.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Customer/
        private Logic.Logic logic = Logic.Logic.GetInstance(new Logic.DbConnector());

        public ActionResult Index() //auto assumes is customer by default.
        {
            bool prevLogIn = false;
            UserL user = new UserL();

            if (Session["User"] != null)
            {
                //already logined as customer when navigate to this page
                prevLogIn = true;
                user = (UserL)Session["User"];
                ViewBag.type = user.IsAdmin;
                ViewBag.email = user.Email;
                ViewBag.password = user.Password;
                ViewBag.name = user.Name;

            }
            else
            {
                prevLogIn = false;
            }

            ViewBag.loggedIn = prevLogIn;

            return View();
        }
        public string LogInAjax(string email, string password)
        {
            string status = "";
            bool existing = false;
            bool logIn = false;

            UserL user = new UserL();

            existing = logic.CheckIfIsExistingUser(email);

            if (existing)
            {
                logIn = logic.UserLogIn(email, password);

                if (logIn)
                    status = "Successfully log in!";
                else
                    status = "Log in failed";
            }
            else
            {
                status = "Email address not found.";
            }


            // keep in session if log in successful
            if (logIn)
            {
                user = logic.GetUser(email);
                Session["User"] = user;
            }

            return status;
        }
        public string SignUpAjax(string name, string gender, string password, string DOB, string email)
        {
            string status = "";
            bool signUp = false;
            bool existing = false;
            UserL user = new UserL();

            existing = logic.CheckIfIsExistingUser(email);

            if (!existing)
            {

                string userType;
                string[] split = email.Split('@');
                if (split[1] == "musicstore.com")
                    userType = "Y"; // ADMIN
                else
                    userType = "N"; // CUSTOMER


                signUp = logic.CreateUser(name, gender, password, DOB, email, userType);

                if (signUp)
                {
                    status = "Successfully sign up!";
                    user = logic.GetUser(email);
                    Session["User"] = user;
                }
                else
                    status = "Sign up failed";
            }
            else
            {
                status = "User is already a member";
            }

            return status;
        }
        public string UpdateProfileAjax(string oldEmail, string name, string gender, DateTime DOB, string email, string password, string currPassword, bool updatePassword)
        {
            string status = "";
            bool update = false;
            bool passwordMatch = false;
            string day = (DOB.Day).ToString();
            string month = (DOB.Month).ToString();
            string year = (DOB.Year).ToString();
            string DOB2 = day + "/" + month + "/" + year;
            UserL cust = new UserL();

            passwordMatch = logic.UserLogIn(oldEmail, currPassword);

            if (!passwordMatch)
            {
                status = "Wrong password!";
            }
            else
            {
                cust = logic.GetUser(oldEmail);
                update = logic.UpdateUser(name, gender, DOB2, email, password, updatePassword);

                if (update)
                    status = "Successfully updated profile";
                else
                    status = "Failed to update";
            }

            return status;
        }
        public ActionResult LogOut()
        {
            if (Session["User"] != null)
                Session["User"] = null;

            return View();
        }
        public ActionResult EditProfile()
        {
            UserL userL = new UserL();
            string email;

            if (Session["User"] != null)
            {
                userL = (UserL)Session["User"];
                email = userL.Email;

                userL = logic.GetUser(email);

                Session["User"] = userL;
                ViewBag.user = userL;
                string date = Logic.Utilities.ToHtmlDateString(userL.DOB);
                ViewBag.DOB = date;
            }

            return View();
        }

    }
}
